<?php

namespace app\admin\controller\dormitory;

use app\admin\model\Admin;
use app\admin\model\DormitoryBuilding;
use app\admin\model\DormitoryDorm;
use app\admin\model\DormitoryDormBed;
use app\admin\model\DormitoryGarden;
use app\common\controller\Backend;
use think\Db;
use think\Log;

class Statistics extends Backend
{

    protected $searchFields = '';
    protected $noNeedRight = [];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = DormitoryBuilding::where($where)->count();

            $list = DormitoryBuilding::alias('a')
                ->join('__DORMITORY_GARDEN__ b', 'b.number = a.garden_number', 'left')
                ->join('__DORMITORY_DORM__ c', 'c.building_number = a.number and c.garden_number = a.garden_number', 'left')
                ->join('__DORMITORY_DORM_BED__ d', 'd.dorm_number = c.number and d.building_number = a.number and d.garden_number = a.garden_number', 'left')
                ->where('c.status', DormitoryDorm::NORMAL)
                ->where($where)
                ->field('a.id,b.name garden_name,a.name building_name,COUNT(d.id) total_count,
                        SUM(CASE WHEN d.status = 2 THEN 1 ELSE 0 END) used_count, 
                        SUM(CASE WHEN d.status = 2 AND c.type = 1 THEN 1 ELSE 0 END) used_boy_count, 
                        SUM(CASE WHEN d.status = 2 AND c.type = 2 THEN 1 ELSE 0 END) used_girl_count,
                        SUM(CASE WHEN d.status = 1 THEN 1 ELSE 0 END) free_count, 
                        SUM(CASE WHEN d.status = 1 AND c.type = 1 THEN 1 ELSE 0 END) free_boy_count, 
                        SUM(CASE WHEN d.status = 1 AND c.type = 2 THEN 1 ELSE 0 END) free_girl_count')
                ->group('a.id')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    public function building($ids = null)
    {
        $building = DormitoryBuilding::alias('a')
            ->join('__DORMITORY_GARDEN__ b', 'b.number = a.garden_number', 'left')
            ->field('a.*,b.name garden_name')
            ->where('a.id', $ids)
            ->find();

        if ($building) {
            $dorm_list = DormitoryDorm::where('garden_number', $building['garden_number'])
                ->where('building_number', $building['number'])
                ->select();

            $list = [];
            foreach ($dorm_list as $key => $value) {
                if ($value['bed_used_count'] == 0) {
                    $value['color'] = 1;
                } else if ($value['bed_used_count'] < $value['bed_count']) {
                    $value['color'] = 2;
                } else {
                    $value['color'] = 3;
                }
                $list[$value['floor']][] = $value;
            }

            krsort($list);

            $this->view->assign('building', $building);
            $this->view->assign('list', $list);

        }

        return $this->view->fetch();

    }

    public function dorm($ids = null)
    {
        $this->assignconfig('ids', $ids);

        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = DormitoryDormBed::alias('a')
                ->join('__DORMITORY_DORM__ b', 'b.number = a.dorm_number and b.building_number = a.building_number and b.garden_number = a.garden_number', 'left')
                ->join('__DORMITORY_GARDEN__ c', 'c.number = a.garden_number', 'left')
                ->join('__DORMITORY_BUILDING__ d', 'd.number = a.building_number and d.garden_number = a.garden_number', 'left')
                ->join('__STUDENT__ e', 'e.id = a.student_id', 'left')
                ->where('b.id', $ids)
                ->field('a.*,c.name garden_name,d.name building_name,e.number student_number,e.name as student_name,e.id_number as student_id_number')
                ->order($sort, $order)
                ->select();

            if ($list) {
                $list = collection($list)->append(['status_text'])->toArray();
            }

            $result = array("total" => count($list), "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();

    }

}
